package com.example.opengl.camera.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.*
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.opengl.R
import com.example.opengl.camera.dialog.FilterAdapter.FilterItemSelectListener
import com.example.opengl.camera.filter.FilterType

class PopFilterView(private val context: Context) : FilterItemSelectListener {
    private val TAG = PopFilterView::class.java.simpleName
    private var popupWindow: PopupWindow? = null
    private var popupWindowView: View? = null
    var recyclerView: RecyclerView? = null
    private val filterBeans: MutableList<FilterBean> = ArrayList()
    private val images = intArrayOf(
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance,
        R.mipmap.ic_short_video_filter_romance
    )
    private val names = arrayOf(
        "温暖", "寒冷", "底片", "旧照", "梦幻", "浮雕", "黑白"
    )

    private fun initFilterData() {
        for (i in names.indices) {
            val filterBean = FilterBean()
            filterBean.filterName = names[i]
            filterBean.filterImageId = images[i]
            //            if(i==0){
//                filterBean.setSelect(true);
//            }
            filterBeans.add(filterBean)
        }
    }

    /**
     * 初始化
     */
    @SuppressLint("ClickableViewAccessibility")
    fun initPopupWindow() {
        if (popupWindowView != null) {
            popupWindow!!.dismiss()
        }
        popupWindowView = LayoutInflater.from(context).inflate(R.layout.shortvideo_record_video_pop_filter_view, null)
        popupWindow =
            PopupWindow(popupWindowView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
        recyclerView = popupWindowView?.findViewById(R.id.recycler_view)
        popupWindow!!.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        popupWindow!!.isOutsideTouchable = true
        popupWindow!!.setBackgroundDrawable(BitmapDrawable())

        // 设置背景半透明
        popupWindow!!.setOnDismissListener(popupDismissListener())
        this.popupWindowView?.setOnTouchListener { _, _ -> false }
        initView()
    }

    var filterAdapter: FilterAdapter? = null
    var iv_switch: ImageView? = null
    @SuppressLint("NotifyDataSetChanged")
    private fun initView() {
        if (filterAdapter == null) {
            filterAdapter = FilterAdapter(context, filterBeans)
        }
        filterAdapter!!.setFilterItemSelectListener(this)
        recyclerView!!.adapter = filterAdapter
        val linearLayoutManager = LinearLayoutManager(
            context
        )
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        recyclerView!!.layoutManager = linearLayoutManager
        iv_switch = popupWindowView!!.findViewById(R.id.iv_switch)
        iv_switch?.setOnClickListener(View.OnClickListener { v: View? ->
            if (filterSelectListener != null) {
                iv_switch?.setSelected(false)
                filterSelectListener!!.filterSelect(null, FilterType.NONE)
                for (filterBean in filterBeans) {
                    filterBean.isSelect = false
                }
                filterAdapter!!.notifyDataSetChanged()
                filterSelectListener!!.recoverFilter()
            }
        })
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    fun backgroundAlpha(bgAlpha: Float) {
        val lp = (context as Activity).window.attributes
        lp.alpha = bgAlpha // 0.0-1.0
        context.window.attributes = lp
    }

    internal inner class popupDismissListener : PopupWindow.OnDismissListener {
        override fun onDismiss() {
            backgroundAlpha(1f)
            if (filterSelectListener != null) {
                filterSelectListener!!.filterDimss()
            }
        }
    }

    fun dimss() {
        if (popupWindow != null) {
            popupWindow!!.dismiss()
        }
    }

    val isShowing: Boolean
        get() = popupWindow!!.isShowing

    fun show(rootView: View?) {
        if (popupWindow != null && !popupWindow!!.isShowing) {
            popupWindow!!.showAtLocation(
                rootView,
                Gravity.BOTTOM, 0, 0
            )
        }
    }

    interface FilterSelectListener {
        fun filterSelect(filterBean: FilterBean?, pos: FilterType?)
        fun filterDimss()
        fun recoverFilter()
    }

    var filterSelectListener: FilterSelectListener? = null
    @JvmName("setFilterOnClick")
    fun setFilterSelectListener(filterSelectListener: FilterSelectListener?) {
        this.filterSelectListener = filterSelectListener
    }

    override fun filterItemSelect(filterBean: FilterBean?, pos: Int) {
        if (filterSelectListener != null) {
            // 滤镜列表中多了原片选项，这里pos+1
            filterSelectListener!!.filterSelect(filterBean, getFilterByIndex(pos))
            iv_switch!!.isSelected = true
        }
        //        dimss();
    }

    fun getFilterByIndex(index: Int): FilterType {
        return mTypes[index]
    }

    private val mTypes = arrayOf( //温暖
        FilterType.WARM,  //寒冷
        FilterType.COOL,  //底片
        FilterType.NEGATIVE,  //旧照
        FilterType.OLD,  //梦幻
        FilterType.DREAM,  //浮雕
        FilterType.RELIEF,  //黑白
        FilterType.BLACK_AND_WHITE
    )

    init {
        initFilterData()
        initPopupWindow()
    }
}